

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>pysimm.apps.random_walk &mdash; pysimm 0.2 documentation</title>
  

  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  

  

  
    <link rel="top" title="pysimm 0.2 documentation" href="../../../index.html"/>
        <link rel="up" title="pysimm" href="../../pysimm.html"/> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> pysimm
          

          
          </a>

          
            
            
              <div class="version">
                0.2
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
                <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../pysimm.html">pysimm package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../../../index.html">pysimm</a>
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          





<div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../../../index.html">Docs</a> &raquo;</li>
      
          <li><a href="../../index.html">Module code</a> &raquo;</li>
      
          <li><a href="../../pysimm.html">pysimm</a> &raquo;</li>
      
    <li>pysimm.apps.random_walk</li>
      <li class="wy-breadcrumbs-aside">
        
          
        
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for pysimm.apps.random_walk</h1><div class="highlight"><pre>
<span></span><span class="c1"># ******************************************************************************</span>
<span class="c1"># pysimm.apps.random_walk module</span>
<span class="c1"># ******************************************************************************</span>
<span class="c1">#</span>
<span class="c1"># psuedo random walk algorithm written using pysimm tools</span>
<span class="c1">#</span>
<span class="c1"># ******************************************************************************</span>
<span class="c1"># License</span>
<span class="c1"># ******************************************************************************</span>
<span class="c1"># The MIT License (MIT)</span>
<span class="c1">#</span>
<span class="c1"># Copyright (c) 2016 Michael E. Fortunato, Coray M. Colina</span>
<span class="c1">#</span>
<span class="c1"># Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="c1"># of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="c1"># in the Software without restriction, including without limitation the rights</span>
<span class="c1"># to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="c1"># copies of the Software, and to permit persons to whom the Software is</span>
<span class="c1"># furnished to do so, subject to the following conditions:</span>
<span class="c1">#</span>
<span class="c1"># The above copyright notice and this permission notice shall be included in</span>
<span class="c1"># all copies or substantial portions of the Software.</span>
<span class="c1">#</span>
<span class="c1"># THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="c1"># IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="c1"># FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="c1"># AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="c1"># LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="c1"># OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN</span>
<span class="c1"># THE SOFTWARE.</span>


<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">strftime</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">permutations</span><span class="p">,</span> <span class="n">izip</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">pysimm</span> <span class="kn">import</span> <span class="n">system</span><span class="p">,</span> <span class="n">lmps</span><span class="p">,</span> <span class="n">forcefield</span><span class="p">,</span> <span class="n">calc</span>
<span class="kn">from</span> <span class="nn">pysimm</span> <span class="kn">import</span> <span class="n">error_print</span>


<div class="viewcode-block" id="find_last_backbone_vector"><a class="viewcode-back" href="../../../pysimm.apps.random_walk.html#pysimm.apps.random_walk.find_last_backbone_vector">[docs]</a><span class="k">def</span> <span class="nf">find_last_backbone_vector</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">m</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;pysimm.apps.random_walk.find_last_backbone_vector</span>

<span class="sd">    Finds vector between backbone atoms in terminal monomer. Requires current system s, and reference monomer m.</span>

<span class="sd">    Args:</span>
<span class="sd">        s: :class:`~pysimm.system.System` object</span>
<span class="sd">        m: :class:`~pysimm.system.System` object</span>
<span class="sd">    Returns:</span>
<span class="sd">        list of vector components</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">head_pos</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
    <span class="n">tail_pos</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="o">*</span><span class="n">m</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">count</span><span class="p">:]:</span>
        <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;head&#39;</span><span class="p">:</span>
            <span class="n">head_pos</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">y</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">z</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;tail&#39;</span><span class="p">:</span>
            <span class="n">tail_pos</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">y</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">z</span><span class="p">]</span>
    <span class="k">return</span> <span class="p">[</span><span class="n">head_pos</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">tail_pos</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">head_pos</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">tail_pos</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">head_pos</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">-</span> <span class="n">tail_pos</span><span class="p">[</span><span class="mi">2</span><span class="p">]]</span></div>


<div class="viewcode-block" id="copolymer"><a class="viewcode-back" href="../../../pysimm.apps.random_walk.html#pysimm.apps.random_walk.copolymer">[docs]</a><span class="k">def</span> <span class="nf">copolymer</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">nmon</span><span class="p">,</span> <span class="n">s_</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;pysimm.apps.random_walk.copolymer</span>

<span class="sd">    Builds copolymer using random walk methodology using pattern</span>

<span class="sd">    Args:</span>
<span class="sd">        m: list of reference monomer :class:`~pysimm.system.System`s</span>
<span class="sd">        nmon: total number of monomers to add to chain</span>
<span class="sd">        s_: :class:`~pysimm.system.System` in which to build polymer chain (None)</span>
<span class="sd">        settings: dictionary of simulation settings</span>
<span class="sd">        density: density at which to build polymer (0.3)</span>
<span class="sd">        forcefield: :class:`~pysimm.forcefield.Forcefield` object to acquire new force field parameters</span>
<span class="sd">        capped: True/False if monomers are capped</span>
<span class="sd">        unwrap: True to unwrap final system</span>
<span class="sd">        traj: True to build xyz trajectory of polymer growth (True)</span>
<span class="sd">        pattern: list of pattern for monomer repeat units, should match length of m ([1 for _ in range(len(m))])</span>
<span class="sd">        limit: during MD, limit atomic displacement by this max value (LAMMPS ONLY)</span>
<span class="sd">        sim: :class:`~pysimm.lmps.Simulation` object for relaxation between polymer growth</span>
<span class="sd">    Returns:</span>
<span class="sd">        new copolymer :class:`~pysimm.system.System`</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">m</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">m</span><span class="p">]</span>

    <span class="n">settings</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;settings&#39;</span><span class="p">,</span> <span class="p">{})</span>
    <span class="n">density</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;density&#39;</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">)</span>
    <span class="n">f</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;forcefield&#39;</span><span class="p">)</span>
    <span class="n">capped</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;capped&#39;</span><span class="p">)</span>
    <span class="n">unwrap</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;unwrap&#39;</span><span class="p">)</span>
    <span class="n">traj</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;traj&#39;</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
    <span class="n">pattern</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;pattern&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">m</span><span class="p">))])</span>
    <span class="n">limit</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;limit&#39;</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">)</span>
    <span class="n">sim</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;sim&#39;</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">m_</span> <span class="ow">in</span> <span class="n">m</span><span class="p">:</span>
        <span class="n">m_</span><span class="o">.</span><span class="n">add_particle_bonding</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">m_</span><span class="o">.</span><span class="n">particles</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">p</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;H&#39;</span><span class="p">):</span>
                <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">=</span> <span class="s1">&#39;head&#39;</span>
            <span class="k">elif</span> <span class="n">p</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">p</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;T&#39;</span><span class="p">):</span>
                <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">=</span> <span class="s1">&#39;tail&#39;</span>
        <span class="n">m_</span><span class="o">.</span><span class="n">remove_linker_types</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">s_</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">system</span><span class="o">.</span><span class="n">replicate</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">1</span><span class="p">,</span> <span class="n">density</span><span class="o">=</span><span class="n">density</span><span class="o">/</span><span class="n">nmon</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">system</span><span class="o">.</span><span class="n">replicate</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">1</span><span class="p">,</span> <span class="n">s_</span><span class="o">=</span><span class="n">s_</span><span class="p">,</span> <span class="n">density</span><span class="o">=</span><span class="n">density</span><span class="o">/</span><span class="n">nmon</span><span class="p">)</span>
    <span class="k">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1"> monomers added&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%H:%M:%S&#39;</span><span class="p">),</span> <span class="mi">1</span><span class="p">,</span> <span class="n">nmon</span><span class="p">))</span>

    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;head&#39;</span><span class="p">:</span>
            <span class="n">last_head</span> <span class="o">=</span> <span class="n">p</span>

        <span class="k">elif</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;tail&#39;</span><span class="p">:</span>
            <span class="n">last_tail</span> <span class="o">=</span> <span class="n">p</span>

    <span class="k">for</span> <span class="n">m_</span> <span class="ow">in</span> <span class="n">m</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">capped</span><span class="p">:</span>
            <span class="n">m_</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
            <span class="n">m_</span><span class="o">.</span><span class="n">remove_spare_bonding</span><span class="p">()</span>
            <span class="n">m_</span><span class="o">.</span><span class="n">add_particle_bonding</span><span class="p">()</span>

    <span class="n">s</span><span class="o">.</span><span class="n">add_particle_bonding</span><span class="p">()</span>
    
    <span class="k">if</span> <span class="n">traj</span><span class="p">:</span>
        <span class="n">s</span><span class="o">.</span><span class="n">write_xyz</span><span class="p">(</span><span class="s1">&#39;random_walk.xyz&#39;</span><span class="p">)</span>

    <span class="n">temp_nmon</span> <span class="o">=</span> <span class="mi">1</span>

    <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>

        <span class="n">m_</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">m</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m_</span><span class="p">)</span>
        <span class="n">p_</span> <span class="o">=</span> <span class="n">pattern</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">pattern</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p_</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">temp_nmon</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">p_</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">m_</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">m</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m_</span><span class="p">)</span>
            <span class="n">p_</span> <span class="o">=</span> <span class="n">pattern</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">pattern</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p_</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">temp_nmon</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">p_</span> <span class="o">-=</span> <span class="mi">1</span>

        <span class="k">for</span> <span class="n">insert</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">p_</span><span class="p">):</span>

            <span class="n">head</span> <span class="o">=</span> <span class="bp">None</span>
            <span class="n">tail</span> <span class="o">=</span> <span class="bp">None</span>

            <span class="n">backbone_vector</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">last_head</span><span class="o">.</span><span class="n">x</span> <span class="o">-</span> <span class="n">last_tail</span><span class="o">.</span><span class="n">x</span><span class="p">,</span>
                                        <span class="n">last_head</span><span class="o">.</span><span class="n">y</span> <span class="o">-</span> <span class="n">last_tail</span><span class="o">.</span><span class="n">y</span><span class="p">,</span>
                                        <span class="n">last_head</span><span class="o">.</span><span class="n">z</span> <span class="o">-</span> <span class="n">last_tail</span><span class="o">.</span><span class="n">z</span><span class="p">])</span>

            <span class="n">ref_head</span> <span class="o">=</span> <span class="bp">None</span>
            <span class="n">ref_tail</span> <span class="o">=</span> <span class="bp">None</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">m_</span><span class="o">.</span><span class="n">particles</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;head&#39;</span><span class="p">:</span>
                    <span class="n">ref_head</span> <span class="o">=</span> <span class="n">p</span>
                <span class="k">elif</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;tail&#39;</span><span class="p">:</span>
                    <span class="n">ref_tail</span> <span class="o">=</span> <span class="n">p</span>
            <span class="k">if</span> <span class="n">ref_head</span> <span class="ow">and</span> <span class="n">ref_tail</span><span class="p">:</span>
                <span class="n">ref_backbone_vector</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">ref_head</span><span class="o">.</span><span class="n">x</span> <span class="o">-</span> <span class="n">ref_tail</span><span class="o">.</span><span class="n">x</span><span class="p">,</span>
                                                <span class="n">ref_head</span><span class="o">.</span><span class="n">y</span> <span class="o">-</span> <span class="n">ref_tail</span><span class="o">.</span><span class="n">y</span><span class="p">,</span>
                                                <span class="n">ref_head</span><span class="o">.</span><span class="n">z</span> <span class="o">-</span> <span class="n">ref_tail</span><span class="o">.</span><span class="n">z</span><span class="p">])</span>
                <span class="n">rot_matrix</span> <span class="o">=</span> <span class="n">calc</span><span class="o">.</span><span class="n">find_rotation</span><span class="p">(</span><span class="n">ref_backbone_vector</span><span class="p">,</span> <span class="n">backbone_vector</span><span class="p">)</span>
                <span class="n">m_</span><span class="o">.</span><span class="n">rotate</span><span class="p">(</span><span class="n">around</span><span class="o">=</span><span class="n">ref_tail</span><span class="p">,</span> <span class="n">rot_matrix</span><span class="o">=</span><span class="n">rot_matrix</span><span class="p">)</span>
                <span class="n">translation_vector</span> <span class="o">=</span> <span class="p">[</span><span class="n">last_tail</span><span class="o">.</span><span class="n">x</span> <span class="o">-</span> <span class="n">ref_tail</span><span class="o">.</span><span class="n">x</span><span class="p">,</span>
                                      <span class="n">last_tail</span><span class="o">.</span><span class="n">y</span> <span class="o">-</span> <span class="n">ref_tail</span><span class="o">.</span><span class="n">y</span><span class="p">,</span>
                                      <span class="n">last_tail</span><span class="o">.</span><span class="n">z</span> <span class="o">-</span> <span class="n">ref_tail</span><span class="o">.</span><span class="n">z</span><span class="p">]</span>
                <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">m_</span><span class="o">.</span><span class="n">particles</span><span class="p">:</span>
                    <span class="n">p</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="n">translation_vector</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">backbone_vector</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="n">p</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">y</span> <span class="o">+</span> <span class="n">translation_vector</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">backbone_vector</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                    <span class="n">p</span><span class="o">.</span><span class="n">z</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">z</span> <span class="o">+</span> <span class="n">translation_vector</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">backbone_vector</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">print</span><span class="p">(</span><span class="s1">&#39;reference molecule has no head or tail&#39;</span><span class="p">)</span>

            <span class="n">n</span> <span class="o">=</span> <span class="n">m_</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

            <span class="k">if</span> <span class="n">capped</span><span class="p">:</span>
                <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>
                <span class="n">s</span><span class="o">.</span><span class="n">remove_spare_bonding</span><span class="p">()</span>
                <span class="n">s</span><span class="o">.</span><span class="n">add_particle_bonding</span><span class="p">()</span>

            <span class="n">s</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">change_dim</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

            <span class="n">s</span><span class="o">.</span><span class="n">add_particle_bonding</span><span class="p">()</span>

            <span class="n">head</span> <span class="o">=</span> <span class="n">last_head</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="o">*</span><span class="n">n</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">count</span><span class="p">:]:</span>
                <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;tail&#39;</span><span class="p">:</span>
                    <span class="n">tail</span> <span class="o">=</span> <span class="n">p</span>

            <span class="n">s</span><span class="o">.</span><span class="n">make_new_bonds</span><span class="p">(</span><span class="n">head</span><span class="p">,</span> <span class="n">tail</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
            <span class="n">temp_nmon</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1"> monomers added&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%H:%M:%S&#39;</span><span class="p">),</span> <span class="n">temp_nmon</span><span class="p">,</span> <span class="n">nmon</span><span class="p">))</span>

            <span class="k">if</span> <span class="n">unwrap</span><span class="p">:</span>
                <span class="n">s</span><span class="o">.</span><span class="n">unwrap</span><span class="p">()</span>
                
            <span class="k">if</span> <span class="n">sim</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">sim</span> <span class="o">=</span> <span class="n">lmps</span><span class="o">.</span><span class="n">Simulation</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;relax_</span><span class="si">%03d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">temp_nmon</span><span class="p">),</span> <span class="n">log</span><span class="o">=</span><span class="s1">&#39;relax.log&#39;</span><span class="p">,</span> <span class="o">**</span><span class="n">settings</span><span class="p">)</span>
                <span class="n">sim</span><span class="o">.</span><span class="n">add_md</span><span class="p">(</span><span class="n">ensemble</span><span class="o">=</span><span class="s1">&#39;nve&#39;</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="n">limit</span><span class="p">,</span> <span class="o">**</span><span class="n">settings</span><span class="p">)</span>
                <span class="n">sim</span><span class="o">.</span><span class="n">add_min</span><span class="p">(</span><span class="o">**</span><span class="n">settings</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">sim</span><span class="p">,</span> <span class="n">lmps</span><span class="o">.</span><span class="n">Simulation</span><span class="p">):</span>
                <span class="n">sim</span><span class="o">.</span><span class="n">system</span> <span class="o">=</span> <span class="n">s</span>
                <span class="n">sim</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;relax_</span><span class="si">%03d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">temp_nmon</span><span class="p">)</span>
                <span class="n">sim</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">np</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;np&#39;</span><span class="p">))</span>

            <span class="k">if</span> <span class="n">unwrap</span><span class="p">:</span>
                <span class="n">s</span><span class="o">.</span><span class="n">unwrap</span><span class="p">()</span>

            <span class="k">if</span> <span class="n">unwrap</span><span class="p">:</span>
                <span class="n">s</span><span class="o">.</span><span class="n">wrap</span><span class="p">()</span>

            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="o">*</span><span class="n">n</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">count</span><span class="p">:]:</span>
                <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;head&#39;</span><span class="p">:</span>
                    <span class="n">last_head</span> <span class="o">=</span> <span class="n">p</span>
                <span class="k">elif</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;tail&#39;</span><span class="p">:</span>
                    <span class="n">last_tail</span> <span class="o">=</span> <span class="n">p</span>

        <span class="k">if</span> <span class="n">temp_nmon</span> <span class="o">&gt;=</span> <span class="n">nmon</span><span class="p">:</span>
            <span class="k">break</span>
        
        <span class="k">if</span> <span class="n">unwrap</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">s</span><span class="o">.</span><span class="n">unwrap</span><span class="p">():</span>
                <span class="n">error_print</span><span class="p">(</span><span class="s1">&#39;something went wrong&#39;</span><span class="p">)</span>
                <span class="k">return</span> <span class="n">s</span>
    
        <span class="k">if</span> <span class="n">traj</span><span class="p">:</span>
            <span class="n">s</span><span class="o">.</span><span class="n">write_xyz</span><span class="p">(</span><span class="s1">&#39;random_walk.xyz&#39;</span><span class="p">,</span> <span class="n">append</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    
        <span class="k">if</span> <span class="n">unwrap</span><span class="p">:</span>
            <span class="n">s</span><span class="o">.</span><span class="n">wrap</span><span class="p">()</span>
            
    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">p</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">molecules</span><span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">molecule</span><span class="o">.</span><span class="n">tag</span><span class="p">]</span><span class="o">.</span><span class="n">particles</span><span class="p">:</span>
            <span class="n">s</span><span class="o">.</span><span class="n">molecules</span><span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">molecule</span><span class="o">.</span><span class="n">tag</span><span class="p">]</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>

    <span class="n">s</span><span class="o">.</span><span class="n">write_lammps</span><span class="p">(</span><span class="s1">&#39;polymer.lmps&#39;</span><span class="p">)</span>
    <span class="n">s</span><span class="o">.</span><span class="n">unwrap</span><span class="p">()</span>
    <span class="n">s</span><span class="o">.</span><span class="n">write_xyz</span><span class="p">(</span><span class="s1">&#39;polymer.xyz&#39;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">s</span></div>


<div class="viewcode-block" id="random_walk"><a class="viewcode-back" href="../../../pysimm.apps.random_walk.html#pysimm.apps.random_walk.random_walk">[docs]</a><span class="k">def</span> <span class="nf">random_walk</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">nmon</span><span class="p">,</span> <span class="n">s_</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;pysimm.apps.random_walk.random_walk</span>

<span class="sd">    Builds homopolymer using random walk methodology</span>

<span class="sd">    Args:</span>
<span class="sd">        m: reference monomer :class:`~pysimm.system.System`</span>
<span class="sd">        nmon: total number of monomers to add to chain</span>
<span class="sd">        s_: :class:`~pysimm.system.System` in which to build polymer chain (None)</span>
<span class="sd">        extra_bonds: EXPERMINTAL, True if making ladder backbone polymer</span>
<span class="sd">        settings: dictionary of simulation settings</span>
<span class="sd">        density: density at which to build polymer (0.3)</span>
<span class="sd">        forcefield: :class:`~pysimm.forcefield.Forcefield` object to acquire new force field parameters</span>
<span class="sd">        capped: True/False if monomers are capped</span>
<span class="sd">        unwrap: True to unwrap final system</span>
<span class="sd">        traj: True to build xyz trajectory of polymer growth (True)</span>
<span class="sd">        limit: during MD, limit atomic displacement by this max value (LAMMPS ONLY)</span>
<span class="sd">        sim: :class:`~pysimm.lmps.Simulation` object for relaxation between polymer growth</span>
<span class="sd">    Returns:</span>
<span class="sd">        new polymer :class:`~pysimm.system.System`</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">m</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

    <span class="n">extra_bonds</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;extra_bonds&#39;</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>

    <span class="n">settings</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;settings&#39;</span><span class="p">,</span> <span class="p">{})</span>
    <span class="n">density</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;density&#39;</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">)</span>
    <span class="n">f</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;forcefield&#39;</span><span class="p">)</span>
    <span class="n">capped</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;capped&#39;</span><span class="p">)</span>
    <span class="n">unwrap</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;unwrap&#39;</span><span class="p">)</span>
    <span class="n">traj</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;traj&#39;</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
    <span class="n">limit</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;limit&#39;</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">)</span>
    <span class="n">sim</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;sim&#39;</span><span class="p">)</span>

    <span class="n">m</span><span class="o">.</span><span class="n">add_particle_bonding</span><span class="p">()</span>

    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">m</span><span class="o">.</span><span class="n">particles</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">p</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;H&#39;</span><span class="p">):</span>
            <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">=</span> <span class="s1">&#39;head&#39;</span>
        <span class="k">elif</span> <span class="n">p</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">p</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;T&#39;</span><span class="p">):</span>
            <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">=</span> <span class="s1">&#39;tail&#39;</span>

    <span class="n">m</span><span class="o">.</span><span class="n">remove_linker_types</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">s_</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">system</span><span class="o">.</span><span class="n">replicate</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">density</span><span class="o">=</span><span class="n">density</span><span class="o">/</span><span class="n">nmon</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">system</span><span class="o">.</span><span class="n">replicate</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">s_</span><span class="o">=</span><span class="n">s_</span><span class="p">,</span> <span class="n">density</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
    <span class="k">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1"> monomers added&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%H:%M:%S&#39;</span><span class="p">),</span> <span class="mi">1</span><span class="p">,</span> <span class="n">nmon</span><span class="p">))</span>

    <span class="k">if</span> <span class="n">traj</span><span class="p">:</span>
        <span class="n">s</span><span class="o">.</span><span class="n">write_xyz</span><span class="p">(</span><span class="s1">&#39;random_walk.xyz&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">capped</span><span class="p">:</span>
        <span class="n">m</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">m</span><span class="o">.</span><span class="n">remove_spare_bonding</span><span class="p">()</span>
        <span class="n">m</span><span class="o">.</span><span class="n">add_particle_bonding</span><span class="p">()</span>

    <span class="k">for</span> <span class="n">insertion</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nmon</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>

        <span class="n">head</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="n">tail</span> <span class="o">=</span> <span class="bp">None</span>

        <span class="n">backbone_vector</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">find_last_backbone_vector</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">m</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">p</span><span class="p">,</span> <span class="n">p_</span> <span class="ow">in</span> <span class="n">izip</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="o">*</span><span class="n">m</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">count</span><span class="p">:],</span> <span class="n">m</span><span class="o">.</span><span class="n">particles</span><span class="p">):</span>
                <span class="n">p_</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">backbone_vector</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">p_</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">y</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">backbone_vector</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">p_</span><span class="o">.</span><span class="n">z</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">z</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">backbone_vector</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>

        <span class="n">n</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">capped</span><span class="p">:</span>
            <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>
            <span class="n">s</span><span class="o">.</span><span class="n">remove_spare_bonding</span><span class="p">()</span>
            <span class="n">s</span><span class="o">.</span><span class="n">add_particle_bonding</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">extra_bonds</span><span class="p">:</span>
            <span class="n">heads</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="o">*</span><span class="n">n</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">count</span><span class="p">:]:</span>
                <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;head&#39;</span><span class="p">:</span>
                    <span class="n">heads</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="o">*</span><span class="n">n</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">count</span><span class="p">:]:</span>
                <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;head&#39;</span><span class="p">:</span>
                    <span class="n">head</span> <span class="o">=</span> <span class="n">p</span>

        <span class="n">s</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">change_dim</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

        <span class="n">s</span><span class="o">.</span><span class="n">add_particle_bonding</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">extra_bonds</span><span class="p">:</span>
            <span class="n">tails</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="o">*</span><span class="n">n</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">count</span><span class="p">:]:</span>
                <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;tail&#39;</span><span class="p">:</span>
                    <span class="n">tails</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="o">*</span><span class="n">n</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">count</span><span class="p">:]:</span>
                <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">linker</span> <span class="o">==</span> <span class="s1">&#39;tail&#39;</span><span class="p">:</span>
                    <span class="n">tail</span> <span class="o">=</span> <span class="n">p</span>

        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">p</span><span class="o">.</span><span class="n">bonded_to</span><span class="p">:</span>
                <span class="k">print</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">tag</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">head</span> <span class="ow">and</span> <span class="n">tail</span><span class="p">:</span>
            <span class="n">s</span><span class="o">.</span><span class="n">make_new_bonds</span><span class="p">(</span><span class="n">head</span><span class="p">,</span> <span class="n">tail</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
            <span class="k">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1"> monomers added&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%H:%M:%S&#39;</span><span class="p">),</span> <span class="n">insertion</span><span class="o">+</span><span class="mi">2</span><span class="p">,</span> <span class="n">nmon</span><span class="p">))</span>
        <span class="k">elif</span> <span class="n">extra_bonds</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">heads</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">tails</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">h</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">izip</span><span class="p">(</span><span class="n">heads</span><span class="p">,</span> <span class="n">tails</span><span class="p">):</span>
                <span class="n">s</span><span class="o">.</span><span class="n">make_new_bonds</span><span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
            <span class="k">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1"> monomers added&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%H:%M:%S&#39;</span><span class="p">),</span> <span class="n">insertion</span><span class="o">+</span><span class="mi">2</span><span class="p">,</span> <span class="n">nmon</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">print</span><span class="p">(</span><span class="s1">&#39;cannot find head and tail&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">sim</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">sim</span> <span class="o">=</span> <span class="n">lmps</span><span class="o">.</span><span class="n">Simulation</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;relax_</span><span class="si">%03d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">insertion</span><span class="o">+</span><span class="mi">2</span><span class="p">),</span> <span class="n">log</span><span class="o">=</span><span class="s1">&#39;relax.log&#39;</span><span class="p">,</span> <span class="o">**</span><span class="n">settings</span><span class="p">)</span>
            <span class="n">sim</span><span class="o">.</span><span class="n">add_md</span><span class="p">(</span><span class="n">ensemble</span><span class="o">=</span><span class="s1">&#39;nve&#39;</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="n">limit</span><span class="p">,</span> <span class="o">**</span><span class="n">settings</span><span class="p">)</span>
            <span class="n">sim</span><span class="o">.</span><span class="n">add_min</span><span class="p">(</span><span class="o">**</span><span class="n">settings</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">sim</span><span class="p">,</span> <span class="n">lmps</span><span class="o">.</span><span class="n">Simulation</span><span class="p">):</span>
            <span class="n">sim</span><span class="o">.</span><span class="n">system</span> <span class="o">=</span> <span class="n">s</span>
            <span class="n">sim</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;relax_</span><span class="si">%03d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">insertion</span><span class="o">+</span><span class="mi">2</span><span class="p">)</span>
            <span class="n">sim</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">np</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;np&#39;</span><span class="p">))</span>

        <span class="n">s</span><span class="o">.</span><span class="n">unwrap</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">traj</span><span class="p">:</span>
            <span class="n">s</span><span class="o">.</span><span class="n">write_xyz</span><span class="p">(</span><span class="s1">&#39;random_walk.xyz&#39;</span><span class="p">,</span> <span class="n">append</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">unwrap</span><span class="p">:</span>
            <span class="n">s</span><span class="o">.</span><span class="n">wrap</span><span class="p">()</span>
            
    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">particles</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">p</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">molecules</span><span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">molecule</span><span class="o">.</span><span class="n">tag</span><span class="p">]</span><span class="o">.</span><span class="n">particles</span><span class="p">:</span>
            <span class="n">s</span><span class="o">.</span><span class="n">molecules</span><span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">molecule</span><span class="o">.</span><span class="n">tag</span><span class="p">]</span><span class="o">.</span><span class="n">particles</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>

    <span class="n">s</span><span class="o">.</span><span class="n">write_lammps</span><span class="p">(</span><span class="s1">&#39;polymer.lmps&#39;</span><span class="p">)</span>
    <span class="n">s</span><span class="o">.</span><span class="n">unwrap</span><span class="p">()</span>
    <span class="n">s</span><span class="o">.</span><span class="n">write_xyz</span><span class="p">(</span><span class="s1">&#39;polymer.xyz&#39;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">s</span></div>
</pre></div>

           </div>
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2017, https://pysimm.org.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'0.2',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>